package com.wen.controller;

import com.wen.pojo.Books;
import com.wen.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping("/Book")
public class BooksController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBooks();
        model.addAttribute("list",list);
        return "allBook";
    }
    @RequestMapping("/turnAdd")
    public String turnAdd(){
        return "addBooks";
    }
    @RequestMapping("/addBooks")
    public String add(Books books){
        bookService.addBook(books);
        return "redirect:/Book/allBook";
    }
    @RequestMapping("/deleteBooks/{bookid}")
    public String delete(@PathVariable("bookid") String id){
        bookService.deleteBook(id);
        return "redirect:/Book/allBook";
    }
    @RequestMapping("/toUpdateBooks")
    public String update(Model model,String bookid){
        Books books = bookService.queryBookById(bookid);
        System.out.println(books);
        model.addAttribute("books",books);
        System.out.println("toupdate结束了！！！");
        return "updateBooks";
    }

    @RequestMapping("/updateBooks")
    public  String update(Model model,Books books){
        System.out.println("进入了updateBooks");
        System.out.println(books);
        bookService.updateBook(books);
        List<Books> list = bookService.queryAllBooks();
        System.out.println(list);
        model.addAttribute("list",list);
        return "redirect:/Book/allBook";
    }
    @RequestMapping("/turnUpload")
    public  String turnUpload(){
        return "upload";
    }
    @RequestMapping("/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件名
        String uploadFileName = file.getOriginalFilename();
        //如果文件名为空，直接回到首页！
        if("".equals(uploadFileName)) return "redirect:/Book/allBook";
            //上传路径保存设置
        String path = request.getSession().getServletContext().getRealPath("/upload");//??????
            //如果路径不存在，创建一个
        File realpath = new File(path);
        if(!realpath.exists()){
            realpath.mkdir();
        }
        System.out.println("上传文件保存地址："+realpath);
            //文件输入流
        InputStream is = file.getInputStream();
            //文件输入流
        OutputStream os = new FileOutputStream(new File(realpath,uploadFileName));
            //读取写出
        byte[] bytes = new byte[1024];
        int len;
        while((len=is.read(bytes))!=-1){
            os.write(bytes,0,len);
            os.flush();
        }
        is.close();
        os.close();

        return "redirect:/Book/allBook";
    }
}
